package com.web.controller;

import com.aop.SystemLog;
import com.dto.BaseDto;
import com.entity.Customer;
import com.entity.Student;
import com.page.PageInfo;
import com.web.service.CustomerService;
import com.web.service.impl.CustomerServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.ws.rs.Path;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/customerController")
public class CustomerController {
    @Resource
    CustomerService customerService;

    @RequestMapping("/customers")
    @ResponseBody
    public BaseDto<Customer> getAll() throws Exception {
//        System.out.println("getAll");
//       List<Student> students = Arrays.asList(new Student[]{new Student("0901", "张三"), new Student("0902", "李四"), new Student("0901", "王二")});
//       return students;
        return customerService.getAll();
    }

    @SystemLog(module = "会员管理", methods = "根据会员号查询基本信息和关联的营养师")
    @RequestMapping("/customer/{cid}/json")
    @ResponseBody
    public BaseDto<Customer> selectByPrimaryKeyWithDietian(@PathVariable("cid") String cid) throws Exception {
        return customerService.selectByPrimaryKeyWithDietian(cid);
    }


    @SystemLog(module = "会员管理", methods = "根据会员号和营养师姓名搜索营养师")
    @RequestMapping("/dietitian/search/{petternName}/{cid}")
    @ResponseBody
    public BaseDto<String> searchMyDietitian(@PathVariable("cid") String cid,@PathVariable("petternName") String petternName) throws Exception {
        return customerService.searchMyDietitianIds(cid,petternName);
    }

    @SystemLog(module = "会员管理", methods = "分页查询")
    @RequestMapping("/customers/page/json")
    @ResponseBody
    public BaseDto<Customer> getCustomersByPageJson(PageInfo pageInfo) throws Exception {
        String cacheId = "CustomerByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
        return pageInfo.getCurrPage() > 2 ? customerService.getCustomersByPage(pageInfo) :
                customerService.getCustomersByPage(pageInfo, cacheId);
    }


    /**
     * 根据用户提供的用户名模糊搜索找到 用户编号集合
     * @param petternName
     * @return
     * @throws Exception
     */
    @RequestMapping("/cids/search/{petternName}")
    @ResponseBody
        public BaseDto<String> searchCustomerIdsByName(@PathVariable("petternName") String petternName) throws Exception {
            return customerService.searchCustomerIdsByName(petternName);
        }

    @RequestMapping("/customers/page")
    public ModelAndView getCustomersByPage(PageInfo pageInfo) throws Exception {
        BaseDto<Customer> dto = customerService.getCustomersByPage(pageInfo);

        return new ModelAndView("/back/customer/list", "dto", dto);
    }

    //@RequestMapping(name = "/customer",method = RequestMethod.POST)
    @PostMapping("/customer")
    @ResponseBody
    public BaseDto<Customer> add(Customer customer) throws Exception {
        return customerService.add(customer);
    }

    /**
     * 会员充值
     *
     * @param customer
     * @return
     * @throws Exception
     */

    //@RequestMapping(name = "/customer",method = RequestMethod.POST)
    @PatchMapping("/customer/cost")
    @ResponseBody
    public BaseDto<Customer> addCustomerCost(Customer customer) throws Exception {
        return customerService.addCustomerCost(customer.getCid(), customer.getCcost());
    }

    @DeleteMapping("/customer/ttt")
    @ResponseBody
    public BaseDto<Customer> tt(Customer customer) throws Exception {
        BaseDto dto = new BaseDto();
        dto.setMsg("111");
        return dto;
    }

    @PostMapping("/order/{prid}/{smid}/{cid}")
    @ResponseBody
    public BaseDto addOrder(@PathVariable("prid") Integer prid,@PathVariable("smid")String smid,@PathVariable("cid")String cid) throws Exception{
        return customerService.addOrder(prid,smid,cid);
    }

}
